﻿using System;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using HslCommunication.BasicFramework;
using HslCommunication.LogNet;

namespace HslCommunication.Core.Net
{
	/// <summary>
	/// 基于Udp的应答式通信类<br />
	/// Udp - based responsive communication class
	/// </summary>
	// Token: 0x0200013F RID: 319
	public class NetworkUdpBase : NetworkBase
	{
		/// <summary>
		/// 实例化一个默认的方法<br />
		/// Instantiate a default method
		/// </summary>
		// Token: 0x06001BA5 RID: 7077 RVA: 0x000896C8 File Offset: 0x000878C8
		public NetworkUdpBase()
		{
			this.hybirdLock = new SimpleHybirdLock();
			this.ReceiveTimeout = 5000;
			this.ConnectionId = SoftBasic.GetUniqueStringByGuidAndRandom();
		}

		/// <inheritdoc cref="P:HslCommunication.Core.Net.NetworkDoubleBase.IpAddress" />
		// Token: 0x1700057A RID: 1402
		// (get) Token: 0x06001BA6 RID: 7078 RVA: 0x00089724 File Offset: 0x00087924
		// (set) Token: 0x06001BA7 RID: 7079 RVA: 0x0008972C File Offset: 0x0008792C
		public virtual string IpAddress
		{
			get
			{
				return this.ipAddress;
			}
			set
			{
				this.ipAddress = HslHelper.GetIpAddressFromInput(value);
			}
		}

		/// <inheritdoc cref="P:HslCommunication.Core.Net.NetworkDoubleBase.Port" />
		// Token: 0x1700057B RID: 1403
		// (get) Token: 0x06001BA8 RID: 7080 RVA: 0x0008973B File Offset: 0x0008793B
		// (set) Token: 0x06001BA9 RID: 7081 RVA: 0x00089743 File Offset: 0x00087943
		public virtual int Port { get; set; }

		/// <inheritdoc cref="P:HslCommunication.Core.Net.NetworkDoubleBase.ReceiveTimeOut" />
		// Token: 0x1700057C RID: 1404
		// (get) Token: 0x06001BAA RID: 7082 RVA: 0x0008974C File Offset: 0x0008794C
		// (set) Token: 0x06001BAB RID: 7083 RVA: 0x00089754 File Offset: 0x00087954
		public int ReceiveTimeout { get; set; }

		/// <inheritdoc cref="P:HslCommunication.Core.Net.NetworkDoubleBase.ConnectionId" />
		// Token: 0x1700057D RID: 1405
		// (get) Token: 0x06001BAC RID: 7084 RVA: 0x0008975D File Offset: 0x0008795D
		// (set) Token: 0x06001BAD RID: 7085 RVA: 0x00089765 File Offset: 0x00087965
		public string ConnectionId { get; set; }

		/// <summary>
		/// 获取或设置一次接收时的数据长度，默认2KB数据长度，特殊情况的时候需要调整<br />
		/// Gets or sets the length of data received at a time. The default length is 2KB
		/// </summary>
		// Token: 0x1700057E RID: 1406
		// (get) Token: 0x06001BAE RID: 7086 RVA: 0x0008976E File Offset: 0x0008796E
		// (set) Token: 0x06001BAF RID: 7087 RVA: 0x00089776 File Offset: 0x00087976
		public int ReceiveCacheLength { get; set; } = 2048;

		/// <summary>
		/// 核心的数据交互读取，发数据发送到串口上去，然后从串口上接收返回的数据<br />
		/// The core data is read interactively, the data is sent to the serial port, and the returned data is received from the serial port
		/// </summary>
		/// <param name="value">完整的报文内容</param>
		/// <returns>是否成功的结果对象</returns>
		// Token: 0x06001BB0 RID: 7088 RVA: 0x00089780 File Offset: 0x00087980
		public virtual OperateResult<byte[]> ReadFromCoreServer(byte[] value)
		{
			bool flag = !Authorization.nzugaydgwadawdibbas();
			OperateResult<byte[]> result;
			if (flag)
			{
				result = new OperateResult<byte[]>(StringResources.Language.AuthorizationFailed);
			}
			else
			{
				ILogNet logNet = base.LogNet;
				if (logNet != null)
				{
					logNet.WriteDebug(this.ToString(), StringResources.Language.Send + " : " + SoftBasic.ByteToHexString(value));
				}
				this.hybirdLock.Enter();
				try
				{
					IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse(this.IpAddress), this.Port);
					Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
					socket.SendTo(value, value.Length, SocketFlags.None, remoteEP);
					bool flag2 = this.ReceiveTimeout < 0;
					if (flag2)
					{
						this.hybirdLock.Leave();
						result = OperateResult.CreateSuccessResult<byte[]>(new byte[0]);
					}
					else
					{
						socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, this.ReceiveTimeout);
						IPEndPoint ipendPoint = new IPEndPoint(IPAddress.Any, 0);
						EndPoint endPoint = ipendPoint;
						byte[] array = new byte[this.ReceiveCacheLength];
						int num = socket.ReceiveFrom(array, ref endPoint);
						byte[] array2 = Enumerable.ToArray<byte>(Enumerable.Take<byte>(array, num));
						this.hybirdLock.Leave();
						ILogNet logNet2 = base.LogNet;
						if (logNet2 != null)
						{
							logNet2.WriteDebug(this.ToString(), StringResources.Language.Receive + " : " + SoftBasic.ByteToHexString(array2));
						}
						this.connectErrorCount = 0;
						result = OperateResult.CreateSuccessResult<byte[]>(array2);
					}
				}
				catch (Exception ex)
				{
					this.hybirdLock.Leave();
					bool flag3 = this.connectErrorCount < 100000000;
					if (flag3)
					{
						this.connectErrorCount++;
					}
					result = new OperateResult<byte[]>(-this.connectErrorCount, ex.Message);
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.Net.NetworkDoubleBase.IpAddressPing" />
		// Token: 0x06001BB1 RID: 7089 RVA: 0x00089944 File Offset: 0x00087B44
		public IPStatus IpAddressPing()
		{
			Ping ping = new Ping();
			return ping.Send(this.IpAddress).Status;
		}

		/// <inheritdoc />
		// Token: 0x06001BB2 RID: 7090 RVA: 0x0008996D File Offset: 0x00087B6D
		public override string ToString()
		{
			return string.Format("NetworkUdpBase[{0}:{1}]", this.IpAddress, this.Port);
		}

		// Token: 0x0400060F RID: 1551
		private SimpleHybirdLock hybirdLock = null;

		// Token: 0x04000610 RID: 1552
		private int connectErrorCount = 0;

		// Token: 0x04000611 RID: 1553
		private string ipAddress = "127.0.0.1";
	}
}
